using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Linq;

using SY.WebSite.Entity;

namespace SY.WebSite.BLL
{
    public class BizRewardResponse
    {
		public const string ENTITY_KEY = "RewardResponse";
		
        public static int Add(RewardResponse entry)
        {
            int result = (int)Common.GetMapper(ENTITY_KEY, entry).Insert("Insert_RewardResponse", entry);
            if (result > 0)
            {
                Tools.CacheUtil.Remove(ENTITY_KEY + ":" + entry.RewardId);

                IList<Reward> list = BizReward.All();
                list.First(l => l.Id == entry.RewardId).ResponseCount++;
                //Tools.CacheUtil.Insert(BizReward.ENTITY_KEY + ":All", list, 3600);
            }
            return result;
        }

        public static int Update(RewardResponse entry)
        {
            return Common.GetMapper(ENTITY_KEY, entry).Update("Update_RewardResponse", entry);
        }

        public static RewardResponse GetByKey(int id)
        {
            return Common.GetMapper(ENTITY_KEY, id).QueryForObject<RewardResponse>("Select_RewardResponse", id);
        }

        public static int Delete(int id)
        {
           return Common.GetMapper(ENTITY_KEY, id).Delete("Delete_RewardResponse",id);
        }

        public static IEnumerable<RewardResponse> GetListByRewardId(int id)
        {
            string key = ENTITY_KEY + ":" + id;
            if (Tools.CacheUtil.Exists(key))
                return Tools.CacheUtil.Get(key) as IEnumerable<RewardResponse>;
            
            IEnumerable<RewardResponse> list = Common.GetMapper(ENTITY_KEY, id).QueryForList<RewardResponse>("Select_RewardResponse_RewardId", id);
            foreach (RewardResponse response in list) {
                response.Content = Tools.StringUtil.HtmlDecode(response.Content);
            }
            Tools.CacheUtil.Insert(key, list, 3600);
            return list;            
        }
    }
}

	